package com.czy.owner.ui.hotshop;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.adapter.BaseRecyclerAdapter;
import com.czy.owner.adapter.ShoppingCartAdapter;
import com.czy.owner.api.AddCartApi;
import com.czy.owner.api.DeleteCartGoodApi;
import com.czy.owner.api.GoodsRemainCountApi;
import com.czy.owner.api.GoodsSpecApi;
import com.czy.owner.api.ShoppingCartListApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CartGoodsSpecModel;
import com.czy.owner.entity.DeliveryWayEntity;
import com.czy.owner.entity.GoodsEntity;
import com.czy.owner.entity.SelectStoreModel;
import com.czy.owner.entity.ShoppingCartGoodsModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.Subscribe;
import com.czy.owner.rxbus.ThreadMode;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.physicalorder.ConfirmOrderActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.glide.GlideUtils;
import com.czy.owner.widget.NoScrollRecyclerView;
import com.czy.owner.widget.TagsLayout;
import com.czy.owner.widget.UniversalDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.czy.owner.ui.physicalorder.ConfirmOrderActivity.ConfirmOrderJumpType.CART;
import static com.czy.owner.ui.physicalorder.ConfirmOrderActivity.TRANSMISSION_GOODS_TYPE_KEY;

/**
 * Created by asus
 * 购物车页面
 */
public class ShoppingCartActivity extends BaseActivity implements ShoppingCartAdapter.PopShowListener, ShoppingCartAdapter.GoodsSelectListener, ShoppingCartAdapter.CheckAllListener {
    private static final int SELECT_STORE_TYPE = 0x11;
    private static final int SETTLE_TYPE = 0x22;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.cb_check_all)
    CheckBox cbCheckAll;
    @BindView(R.id.tv_goods_price_total)
    TextView tvGoodsPriceTotal;
    @BindView(R.id.tv_settle_delete)
    TextView tvSettleDelete;
    @BindView(R.id.rv_store_goods)
    NoScrollRecyclerView rvStoreGoods;
    @BindView(R.id.ll_store_goods)
    LinearLayout llStoreGoods;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;
    @BindView(R.id.ll_goods)
    LinearLayout llGoods;
    @BindView(R.id.tv_store_name)
    TextView tvStoreName;
    @BindView(R.id.rv_paltform_goods)
    NoScrollRecyclerView rvPaltformGoods;
    @BindView(R.id.ll_paltform_goods)
    LinearLayout llPaltformGoods;
    @BindView(R.id.rv_transfer_goods)
    NoScrollRecyclerView rvTransferGoods;
    @BindView(R.id.ll_transfer_goods)
    LinearLayout llTransferGoods;
    private boolean isShown = false;
    private boolean isCheckAll = false;
    private boolean isCheckAllClick = false;
    private ShoppingCartAdapter storeAdapter;
    private ShoppingCartAdapter platformAdapter;
    private ShoppingCartAdapter transferAdapter;
    private ArrayList<GoodsEntity> storeModels = new ArrayList<>();
    private ArrayList<GoodsEntity> transferModels = new ArrayList<>();

    private ArrayList<GoodsEntity> platformModels = new ArrayList<>();

    private PopupWindow popWnd;
    private ArrayList<TextView> tvTabs = new ArrayList<>();
    private ArrayList<TextView> tvPostageTabs = new ArrayList<>();
    private List<CartGoodsSpecModel.SelectedSpecListBean> specList = new ArrayList<>();
    private List<CartGoodsSpecModel.GoodsListBean> specGroupList = new ArrayList<>();
    private ArrayList<CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean> selectSpecValueListBean = new ArrayList<>();
    private int selectPostageId = 0;
    private int buyCount = 1;
    private int serviceInstallStoreId = 0;
    private boolean needInstall = false;
    private boolean isInstallShow = false;
    private boolean isPostageShow = false;
    private boolean isSelectClickable = true;
    private LinearLayout llNoBindStore;
    private LinearLayout llBindStore;
    private TextView tvServiceStore;
    private TextView tvInstallMoney;
    private ImageView ivSelectStore;
    private String storeName = "";
    private String storeServicePrice = "";
    private boolean isChooseServiceStore = false;
    private List<GoodsEntity> deleteData = new ArrayList<>();
    private List<GoodsEntity> settlementData = new ArrayList<>();
    private boolean isSettleCheckAll = false;
    private boolean isDeleteCheckAll = false;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_shopping_cart;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(getString(R.string.shopping_cart));
        getSubTitle().setText(getString(R.string.edit));

        initStoreRecyclerView();
        initPlatformRecyclerView();
        initTransferRecyclerView();
        getCartList();
        RxBus.getDefault().register(this);
    }

    private void getCartList() {
        ShoppingCartListApi shoppingCartListApi = new ShoppingCartListApi(new HttpOnNextListener<ShoppingCartGoodsModel>() {
            @Override
            public void onNext(ShoppingCartGoodsModel model) {
                setCartModel(model);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<ShoppingCartGoodsModel>>() {
                }.getType();
                BaseResultEntity<ShoppingCartGoodsModel> cacheModel = gson.fromJson(cache, type);
                setCartModel(cacheModel.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
            }
        }, this);
        shoppingCartListApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        shoppingCartListApi.setStoreId(SharedPreferencesUtils.getParam(this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        shoppingCartListApi.setCloseMall(UserHelper.getInstance().isCloseMall() + "");
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(shoppingCartListApi);

    }


    @Subscribe(code = Constants.REFRESH_SHOPPING_CAR, threadMode = ThreadMode.MAIN)
    public void refreshData(CommonEvent shoppingCar) {
        getCartList();
    }

    private void setCartModel(ShoppingCartGoodsModel model) {
        storeModels.clear();
        transferModels.clear();
        platformModels.clear();
        deleteData.clear();
        settlementData.clear();
        isSettleCheckAll = false;
        isDeleteCheckAll = false;
        cbCheckAll.setChecked(false);
        storeAdapter.checkAll(cbCheckAll.isChecked());
        platformAdapter.checkAll(cbCheckAll.isChecked());
        transferAdapter.checkAll(cbCheckAll.isChecked());
        platformAdapter.switchCheckAll();
        storeAdapter.switchCheckAll();
        transferAdapter.switchCheckAll();
        tvGoodsPriceTotal.setText("￥0.00");
        if (model != null && model.getCartStoreList() != null) {
            List<GoodsEntity> cartStoreList = model.getCartStoreList();
            for (int i = 0; i < cartStoreList.size(); i++) {
                cartStoreList.get(i).setBelongsType("store");
            }
            storeModels.addAll(cartStoreList);
            storeAdapter.setList(storeModels);
            if (model.getCartStoreList().size() == 0) {
                llStoreGoods.setVisibility(View.GONE);
            }
        } else {
            llStoreGoods.setVisibility(View.GONE);
        }
        if (model != null && model.getCartSystemList() != null) {
            List<GoodsEntity> cartSystemList = model.getCartSystemList();
            for (int i = 0; i < cartSystemList.size(); i++) {
                if ("store".equals(cartSystemList.get(i).getBelongsType())) {
                    cartSystemList.get(i).setBelongsType("transfer");
                    transferModels.add(cartSystemList.get(i));
                } else {
                    cartSystemList.get(i).setBelongsType("system");
                    platformModels.add(cartSystemList.get(i));
                }
            }
//            platformModels.addAll(cartSystemList);

            transferAdapter.setList(transferModels);

            platformAdapter.setList(platformModels);
            if (platformModels.size() == 0) {
                llPaltformGoods.setVisibility(View.GONE);
            }

            if (transferModels.size() == 0) {
                llTransferGoods.setVisibility(View.GONE);
            }
        } else {
            llPaltformGoods.setVisibility(View.GONE);
            llTransferGoods.setVisibility(View.GONE);
        }
        if (model == null) {
            llGoods.setVisibility(View.GONE);
            llEmpty.setVisibility(View.VISIBLE);
        } else {
            if ((model.getCartStoreList() != null && model.getCartStoreList().size() > 0 && ((int) SharedPreferencesUtils.getParam(ShoppingCartActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0)) != 0) || (model.getCartSystemList() != null && model.getCartSystemList().size() > 0)) {
            } else {
                llGoods.setVisibility(View.GONE);
                llEmpty.setVisibility(View.VISIBLE);
            }
        }
    }

    private void initTransferRecyclerView() {
        transferAdapter = new ShoppingCartAdapter(this, transferModels, deleteData, settlementData, isShown, isCheckAll, isCheckAllClick);
        rvTransferGoods.setLayoutManager(new LinearLayoutManager(this));
        rvTransferGoods.setAdapter(transferAdapter);
        rvTransferGoods.setNestedScrollingEnabled(false);
        transferAdapter.setOnPopShowListener(this);
        transferAdapter.setOnGoodsSelectListener(this);
        transferAdapter.setOnCheckAllListener(this);

        transferAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<GoodsEntity>() {
            @Override
            public void onItemClick(int position, GoodsEntity data, View view) {
                Intent intent = new Intent(ShoppingCartActivity.this, GoodsDetailActivity.class);
                intent.putExtra("goodsId", Integer.parseInt(String.valueOf(data.getGoodsId())));
                startActivity(intent);
            }
        });
    }


    private void initStoreRecyclerView() {
        int storeId = (int) SharedPreferencesUtils.getParam(ShoppingCartActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0);
        if (storeId == 0) {
            llStoreGoods.setVisibility(View.GONE);
        }
        String storeName = UserHelper.getInstance().getOwnerInfoModel(ShoppingCartActivity.this).getStoreName();
        tvStoreName.setText(storeName);
        storeAdapter = new ShoppingCartAdapter(this, storeModels, deleteData, settlementData, isShown, isCheckAll, isCheckAllClick);
        rvStoreGoods.setLayoutManager(new LinearLayoutManager(this));
        rvStoreGoods.setAdapter(storeAdapter);
        rvStoreGoods.setNestedScrollingEnabled(false);
        storeAdapter.setOnPopShowListener(this);
        storeAdapter.setOnGoodsSelectListener(this);
        storeAdapter.setOnCheckAllListener(this);

        storeAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<GoodsEntity>() {
            @Override
            public void onItemClick(int position, GoodsEntity data, View view) {
                Intent intent = new Intent(ShoppingCartActivity.this, GoodsDetailActivity.class);
                intent.putExtra("goodsId", Integer.parseInt(String.valueOf(data.getGoodsId())));
                startActivity(intent);

            }
        });
    }


    private void initPlatformRecyclerView() {
        if (UserHelper.getInstance().isCloseMall()) {
            llPaltformGoods.setVisibility(View.GONE);
        }
        platformAdapter = new ShoppingCartAdapter(this, platformModels, deleteData, settlementData, isShown, isCheckAll, isCheckAllClick);
        rvPaltformGoods.setLayoutManager(new LinearLayoutManager(this));
        rvPaltformGoods.setAdapter(platformAdapter);
        rvPaltformGoods.setNestedScrollingEnabled(false);
        platformAdapter.setOnPopShowListener(this);
        platformAdapter.setOnGoodsSelectListener(this);
        platformAdapter.setOnCheckAllListener(this);


        platformAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<GoodsEntity>() {
            @Override
            public void onItemClick(int position, GoodsEntity data, View view) {
                Intent intent = new Intent(ShoppingCartActivity.this, GoodsDetailActivity.class);
                intent.putExtra("goodsId", Integer.parseInt(String.valueOf(data.getGoodsId())));
                startActivity(intent);
            }
        });

    }

    @Override
    protected void updateViews() {

    }

    @OnClick({R.id.toolbar_subtitle, R.id.cb_check_all, R.id.tv_settle_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_subtitle:
                platformAdapter.switchCheckAll();
                storeAdapter.switchCheckAll();
                storeAdapter.switchCheckAll();
                isShown = !isShown;
                double price = 0.0;
                if (isShown) {
                    getSubTitle().setText(getString(R.string.complete));
                    tvSettleDelete.setBackgroundResource(R.color.shop_cart_red);
                    tvSettleDelete.setText("删除(" + deleteData.size() + ")");

                    cbCheckAll.setChecked(isDeleteCheckAll);
                    for (int i = 0; i < deleteData.size(); i++) {
                        if (deleteData.get(i).isHaveMemberPriceFlag()) {
                            price += deleteData.get(i).getStoreMemberPrice() * deleteData.get(i).getCount();
                        } else {
                            price += deleteData.get(i).getGoodsSpecValuesPrice() * deleteData.get(i).getCount();
                        }

                    }
                } else {
                    getSubTitle().setText(getString(R.string.edit));
                    tvSettleDelete.setBackgroundResource(R.color.appThemeColor);
                    tvSettleDelete.setText("去结算(" + settlementData.size() + ")");
                    cbCheckAll.setChecked(isSettleCheckAll);
                    for (int i = 0; i < settlementData.size(); i++) {
                        if (settlementData.get(i).isHaveMemberPriceFlag()) {
                            price += settlementData.get(i).getStoreMemberPrice() * settlementData.get(i).getCount();
                        } else {
                            price += settlementData.get(i).getGoodsSpecValuesPrice() * settlementData.get(i).getCount();
                        }

                    }
                }
                tvGoodsPriceTotal.setText("￥" + String.format("%.2f", price));
                storeAdapter.switchPopShow(isShown);
                platformAdapter.switchPopShow(isShown);
                transferAdapter.switchPopShow(isShown);
                break;
            case R.id.cb_check_all:
                if (isShown) {
                    isDeleteCheckAll = cbCheckAll.isChecked();
                } else {
                    isSettleCheckAll = cbCheckAll.isChecked();
                }
                transferAdapter.checkAll(cbCheckAll.isChecked());
                storeAdapter.checkAll(cbCheckAll.isChecked());
                platformAdapter.checkAll(cbCheckAll.isChecked());
                break;
            case R.id.tv_settle_delete:

                if (isShown) {
                    if (deleteData.size() > 0) {
                        new UniversalDialog(ShoppingCartActivity.this).builder().setTitle("删除商品")
                                .setMsg("确定从购物车移除这" + deleteData.size() + "项商品?")
                                .setPositiveButton("确定", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        DeleteGoodsRequest();
                                    }
                                }).setNegativeButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        }).show();

                    } else {
                        PhoneUtils.ShowToastMessage(ShoppingCartActivity.this, "至少选择一种商品");
                    }
                } else {
                    if (settlementData.size() > 0) {
                        for (int i = 0; i < settlementData.size(); i++) {
                            if (settlementData.get(i).getDeliveryWayId() != 0) {
                                DeliveryWayEntity deliveryWayEntity = new DeliveryWayEntity();
                                deliveryWayEntity.setName(settlementData.get(i).getDeliveryWayName());
                                deliveryWayEntity.setDeliveryWayId(settlementData.get(i).getDeliveryWayId());
                                settlementData.get(i).setDeliveryWay(deliveryWayEntity);
                            }

                            if (settlementData.get(i).getStore() != null && settlementData.get(i).getStore().getStoreId() > 0) {
                                settlementData.get(i).setNeedInstall(true);
                                settlementData.get(i).setInstallStoreName(settlementData.get(i).getStore().getStoreName());
                                settlementData.get(i).setServicePrice(settlementData.get(i).getStore().getServicePrice());
                            }
                            settlementData.get(i).setGoodsLogo(settlementData.get(i).getLogo());
                        }
                        getGoodsRemainCount();

                    } else {
                        PhoneUtils.ShowToastMessage(ShoppingCartActivity.this, "至少选择一种商品");
                    }
                }


                break;
        }
    }

    private void DeleteGoodsRequest() {
        DeleteCartGoodApi deleteCartGoodsApi = new DeleteCartGoodApi(new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                PhoneUtils.ShowToastMessage(ShoppingCartActivity.this, "删除商品成功");
                tvSettleDelete.setText("删除(0)");
                getCartList();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
            }
        }, this);
        deleteCartGoodsApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        StringBuilder shoppingCartIdsBuilder = new StringBuilder();
        for (int i = 0; i < deleteData.size(); i++) {
            if (i == deleteData.size() - 1) {
                shoppingCartIdsBuilder.append(deleteData.get(i).getShoppingCartId());
            } else {
                shoppingCartIdsBuilder.append(deleteData.get(i).getShoppingCartId() + ",");
            }

        }
        deleteCartGoodsApi.setShoppingCartIds(shoppingCartIdsBuilder + "");
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(deleteCartGoodsApi);
    }

    @Override
    public void popShow(GoodsEntity model, final View v) {
        long goodsId = model.getGoodsId();
        String specGroupKey = model.getSpecGroupKey();
        int shoppingCartId = model.getShoppingCartId();
        GoodsSpecApi goodsSpecApi = new GoodsSpecApi(new HttpOnNextListener<CartGoodsSpecModel>() {
            @Override
            public void onNext(CartGoodsSpecModel model) {
                initPopWindow(model, v);
            }


            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
            }
        }, this);
        goodsSpecApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        goodsSpecApi.setGoodsId(goodsId + "");
        goodsSpecApi.setShoppingCartId(shoppingCartId + "");
        goodsSpecApi.setSpecGroupKey(specGroupKey);
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(goodsSpecApi);
    }

    private void initPopWindow(final CartGoodsSpecModel model, View v) {
        MyLog.e("asus", "pop");
        specList = model.getSelectedSpecList();
        specGroupList = model.getGoodsList();
        selectPostageId = 0;
        serviceInstallStoreId = 0;
        tvTabs.clear();
        selectSpecValueListBean.clear();
        tvPostageTabs.clear();
        if (model != null) {
            View contentView = LayoutInflater.from(ShoppingCartActivity.this).inflate(R.layout.pop_goods_standard, null);
            popWnd = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            contentView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (popWnd != null && popWnd.isShowing()) {
                        popWnd.dismiss();
                    }
                    return false;
                }
            });
            ColorDrawable cd = new ColorDrawable(0x77000000);
            popWnd.setBackgroundDrawable(cd);
            // 设置点击窗口外边窗口消失
            popWnd.setOutsideTouchable(true);
            // 设置此参数获得焦点，否则无法点击
            popWnd.setFocusable(true);
            popWnd.setAnimationStyle(R.style.ShoppingCartPopupAnimation);
            popWnd.setClippingEnabled(true);
            LinearLayout llContainer = (LinearLayout) contentView.findViewById(R.id.ll_container);
            final TextView tvSure = (TextView) contentView.findViewById(R.id.tv_sure);
            final TextView tvPopChooseSpec = (TextView) contentView.findViewById(R.id.tv_pop_choose_spec);
            final TextView tvGoodsPrice = (TextView) contentView.findViewById(R.id.tv_goods_price);
            final TextView tvMemberPrice = (TextView) contentView.findViewById(R.id.tv_member_price);
            final ImageView ivPopGoodsDetail = (ImageView) contentView.findViewById(R.id.iv_pop_goods_detail);
            ImageView ivGoodsMinus = (ImageView) contentView.findViewById(R.id.iv_goods_minus);
            final TextView tvGoodsCount = (TextView) contentView.findViewById(R.id.tv_goods_count);
            ImageView ivGoodsAdd = (ImageView) contentView.findViewById(R.id.iv_goods_add);
            if (specList.size() == 0) {
                tvPopChooseSpec.setText("无规格");
            }
            for (int i = 0; i < specList.size(); i++) {
                View addView = LayoutInflater.from(ShoppingCartActivity.this).inflate(R.layout.include_goods_standard, null);
                TagsLayout tagsClassify = (TagsLayout) addView.findViewById(R.id.tags_classify);
                TextView tvClassifyName = (TextView) addView.findViewById(R.id.tv_classify_name);
                ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tvClassifyName.setText(specList.get(i).getSpecName());
                for (int j = 0; j < specList.get(i).getSpecValueList().size(); j++) {
                    View tagView = View.inflate(ShoppingCartActivity.this, R.layout.item_screen_brand, null);
                    TextView textView = (TextView) tagView.findViewById(R.id.tv_screen_item);
                    CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean tag = specList.get(i).getSpecValueList().get(j);
                    textView.setText(tag.getSpecValue());
                    textView.setTag(tag);
                    tvTabs.add(textView);

                    tagView.setTag(false);
                    tagView.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            TextView tv = (TextView) v.findViewById(R.id.tv_screen_item);
                            updateSelectTabs(tv, tvGoodsPrice, tvPopChooseSpec, ivPopGoodsDetail, model, tvMemberPrice);
                        }
                    });
                    tagsClassify.addView(tagView, lp);
                }
                llContainer.addView(addView);
            }

            isInstallShow = model.getGoods().isInstall();
            isPostageShow = model.getGoods().isPostageFree();
            if (isInstallShow) {
                View chooseServiceView = LayoutInflater.from(ShoppingCartActivity.this).inflate(R.layout.include_service_choose, null);
                final CartGoodsSpecModel.GoodsBean.InstallStoreBean installStore = model.getGoods().getInstallStore();
                RadioGroup rgChooseService = (RadioGroup) chooseServiceView.findViewById(R.id.rg_choose_service);
                final LinearLayout llStoreInfo = (LinearLayout) chooseServiceView.findViewById(R.id.ll_store_info);
                llNoBindStore = (LinearLayout) chooseServiceView.findViewById(R.id.ll_no_bind_store);
                final LinearLayout llChooseStore = (LinearLayout) chooseServiceView.findViewById(R.id.ll_choose_store);
                llBindStore = (LinearLayout) chooseServiceView.findViewById(R.id.ll_bind_store);
                tvServiceStore = (TextView) chooseServiceView.findViewById(R.id.tv_service_store);
                tvInstallMoney = (TextView) chooseServiceView.findViewById(R.id.tv_install_money);
                RadioButton rbNeedService = (RadioButton) chooseServiceView.findViewById(R.id.rb_need_service);
                RadioButton rbNoService = (RadioButton) chooseServiceView.findViewById(R.id.rb_no_service);
                ivSelectStore = (ImageView) chooseServiceView.findViewById(R.id.iv_select_store);
                llStoreInfo.setVisibility(View.VISIBLE);
                if (installStore != null) {
                    if (model.getGoods().getServiceInstallStoreId() == 0) {
                        //已绑定选择了无需安装
                        needInstall = false;
                        rbNoService.setChecked(true);
                        llNoBindStore.setVisibility(View.GONE);
                        llBindStore.setVisibility(View.GONE);
                    } else if (model.getGoods().getServiceInstallStoreId() == (int) SharedPreferencesUtils.getParam(ShoppingCartActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0)) {//已绑定选择了需安装
                        //已绑定选择了需要安装
                        needInstall = true;
                        rbNeedService.setChecked(true);
                        isSelectClickable = false;
                        serviceInstallStoreId = installStore.getStoreId();
                        llNoBindStore.setVisibility(View.GONE);
                        llBindStore.setVisibility(View.VISIBLE);
                        String title = "参考安装费: " + "<font color='#09c0c7'>" + "￥" + model.getGoods().getInstallStore().getServicePrice() + "</font>";
                        tvServiceStore.setText("服务门店:" + installStore.getStoreName());
                        tvInstallMoney.setText(Html.fromHtml(title));
                        ivSelectStore.setVisibility(View.GONE);
                    } else {
                        //未绑定选择了需要安装
                        needInstall = true;
                        rbNeedService.setChecked(true);
                        isSelectClickable = true;
                        serviceInstallStoreId = installStore.getStoreId();
                        llNoBindStore.setVisibility(View.GONE);
                        llBindStore.setVisibility(View.VISIBLE);
                        String title = "参考安装费: " + "<font color='#09c0c7'>" + "￥" + model.getGoods().getInstallStore().getServicePrice() + "</font>";
                        tvServiceStore.setText("服务门店:" + installStore.getStoreName());
                        tvInstallMoney.setText(Html.fromHtml(title));
                        ivSelectStore.setVisibility(View.VISIBLE);
                    }

                } else {
                    needInstall = false;
                    rbNoService.setChecked(true);
                    llNoBindStore.setVisibility(View.GONE);
                    llBindStore.setVisibility(View.GONE);
                }


                llChooseStore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(ShoppingCartActivity.this, SelectServiceStoreActivity.class);
                        intent.putExtra("goodsId", model.getGoods().getGoodsId() + "");
                        startActivityForResult(intent, SELECT_STORE_TYPE);
                    }
                });
                llBindStore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isSelectClickable) {
                            Intent intent = new Intent(ShoppingCartActivity.this, SelectServiceStoreActivity.class);
                            intent.putExtra("goodsId", model.getGoods().getGoodsId() + "");
                            startActivityForResult(intent, SELECT_STORE_TYPE);
                        }
                    }
                });
                rgChooseService.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch (checkedId) {
                            case R.id.rb_need_service:
                                needInstall = true;
                                llStoreInfo.setVisibility(View.VISIBLE);
                                if (installStore != null) {
                                    //选择过门店或者已绑定门店
                                    if (model.getGoods().getServiceInstallStoreId() == 0 || model.getGoods().getServiceInstallStoreId() == (int) SharedPreferencesUtils.getParam(ShoppingCartActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0))
                                    //已绑定类型
                                    {
                                        isSelectClickable = false;
                                        serviceInstallStoreId = installStore.getStoreId();
                                        llNoBindStore.setVisibility(View.GONE);
                                        llBindStore.setVisibility(View.VISIBLE);
                                        String title = "参考安装费: " + "<font color='#09c0c7'>" + "￥" + model.getGoods().getInstallStore().getServicePrice() + "</font>";
                                        tvServiceStore.setText("服务门店:" + installStore.getStoreName());
                                        tvInstallMoney.setText(Html.fromHtml(title));
                                        ivSelectStore.setVisibility(View.GONE);
                                    } else {//未绑定类型
                                        if (isChooseServiceStore) {
                                            isChooseServiceStore = true;
                                            llNoBindStore.setVisibility(View.GONE);
                                            llBindStore.setVisibility(View.VISIBLE);
                                            ivSelectStore.setVisibility(View.VISIBLE);
                                            tvServiceStore.setText(storeName);
                                            tvInstallMoney.setText(Html.fromHtml(storeServicePrice));
                                        } else {
                                            isSelectClickable = true;
                                            serviceInstallStoreId = installStore.getStoreId();
                                            llNoBindStore.setVisibility(View.GONE);
                                            llBindStore.setVisibility(View.VISIBLE);
                                            String title = "参考安装费: " + "<font color='#09c0c7'>" + "￥" + model.getGoods().getInstallStore().getServicePrice() + "</font>";
                                            tvServiceStore.setText("服务门店:" + installStore.getStoreName());
                                            tvInstallMoney.setText(Html.fromHtml(title));
                                            ivSelectStore.setVisibility(View.VISIBLE);
                                        }

                                    }
                                } else if (serviceInstallStoreId != 0 && installStore == null) {
                                    //未绑定类型已进行二次选择门店操作
                                    llNoBindStore.setVisibility(View.GONE);
                                    llBindStore.setVisibility(View.VISIBLE);
                                    ivSelectStore.setVisibility(View.VISIBLE);
                                    tvServiceStore.setText(storeName);
                                    tvInstallMoney.setText(Html.fromHtml(storeServicePrice));
                                } else {
                                    //未绑定类型未进行2次操作
                                    llNoBindStore.setVisibility(View.VISIBLE);
                                    llBindStore.setVisibility(View.GONE);
                                }
                                break;
                            case R.id.rb_no_service:
                                needInstall = false;
                                llStoreInfo.setVisibility(View.GONE);
                                break;
                        }
                    }
                });
                llContainer.addView(chooseServiceView);
            }
            if (!isPostageShow) {
                List<CartGoodsSpecModel.DeliveryWayListBean> deliveryWayList = model.getDeliveryWayList();
                View addView = LayoutInflater.from(ShoppingCartActivity.this).inflate(R.layout.include_goods_standard, null);
                TagsLayout tagsClassify = (TagsLayout) addView.findViewById(R.id.tags_classify);
                TextView tvClassifyName = (TextView) addView.findViewById(R.id.tv_classify_name);
                ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tvClassifyName.setText("配送方式");
                if (deliveryWayList.size() == 1) {
                    selectPostageId = deliveryWayList.get(0).getDeliveryWayId();
                } else {
                    if (deliveryWayList.size() > 1) {
                        for (int j = 0; j < deliveryWayList.size(); j++) {
                            View tagView = View.inflate(ShoppingCartActivity.this, R.layout.item_screen_brand, null);
                            TextView textView = (TextView) tagView.findViewById(R.id.tv_screen_item);
                            CartGoodsSpecModel.DeliveryWayListBean deliveryWayListBean = deliveryWayList.get(j);
                            textView.setText(deliveryWayListBean.getName());
                            textView.setTag(deliveryWayListBean.getDeliveryWayId());
                            if (deliveryWayListBean.isSelected()) {
                                selectPostageId = deliveryWayListBean.getDeliveryWayId();
                            }
                            tvPostageTabs.add(textView);
                            tagView.setTag(false);
                            tagView.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    TextView tv = (TextView) v.findViewById(R.id.tv_screen_item);
                                    updatePostageView(tv);
                                }
                            });
                            tagsClassify.addView(tagView, lp);
                        }
                        llContainer.addView(addView);
                    }

                }

            }

            buyCount = model.getGoods().getCount();
            ivGoodsMinus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (buyCount > 1) {
                        buyCount--;
                        tvGoodsCount.setText(buyCount + "");
                    } else {
                        buyCount = 1;
                    }
                }
            });
            ivGoodsAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    buyCount++;
                    tvGoodsCount.setText(buyCount + "");
                }
            });
            tvSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AddToCartRequest(model);

                }
            });
            initPopTabs(tvGoodsPrice, tvPopChooseSpec, tvGoodsCount, ivPopGoodsDetail, model, tvMemberPrice);
            popWnd.showAtLocation(v, Gravity.BOTTOM, 0, 0);

        }
    }


    private void updateSelectTabs(TextView tv, TextView tvGoodsPrice, TextView tvPopChooseSpec, ImageView ivPopGoodsDetail, CartGoodsSpecModel model, TextView tvMemberPrice) {
        CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean tag = (CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean) tv.getTag();
        if (!selectSpecValueListBean.contains(tag)) {
            for (int i = 0; i < selectSpecValueListBean.size(); i++) {
                if (selectSpecValueListBean.get(i).getSpecId() == tag.getSpecId()) {
                    selectSpecValueListBean.remove(i);
                    break;
                }
            }
            selectSpecValueListBean.add(tag);
        }
        int tagSpecId = tag.getSpecId();
        int tagSpecValuesId = tag.getSpecValuesId();
        for (int i = 0; i < tvTabs.size(); i++) {
            CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean tabsTag = (CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean) tvTabs.get(i).getTag();
            if (tabsTag.getSpecId() == tagSpecId) {
                if (tagSpecValuesId == tabsTag.getSpecValuesId()) {
                    tvTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeColor));
                    tvTabs.get(i).setBackgroundResource(R.drawable.shape_app_theme_frame);
                } else {
                    tvTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeSubTitleColor));
                    tvTabs.get(i).setBackgroundResource(R.drawable.shape_gray_frame);
                }
            }
        }
        //1:15,2:16
        Collections.sort(selectSpecValueListBean, new SortBySpecId());
        StringBuilder keyGroup = new StringBuilder();
        StringBuilder specSB = new StringBuilder();
        for (int i = 0; i < selectSpecValueListBean.size(); i++) {
            if (i == selectSpecValueListBean.size() - 1) {
                keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId());
                specSB.append(selectSpecValueListBean.get(i).getSpecValue());
            } else {
                keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId() + ",");
                specSB.append(selectSpecValueListBean.get(i).getSpecValue() + ",");
            }

        }
        if (selectSpecValueListBean.size() > 0) {
            boolean isContaineSpec = false;
            for (int i = 0; i < specGroupList.size(); i++) {

                if (specGroupList.get(i).getGoodsSpecId().equals((keyGroup + ""))) {
                    tvGoodsPrice.setText("￥" + specGroupList.get(i).getGoodsSpecValuesPrice());
                    if (specGroupList.get(i).isHaveMemberPriceFlag()) {
                        tvMemberPrice.setText("会员价: ¥" + String.format("%.2f", specGroupList.get(i).getStoreMemberPrice()));
                    } else {
                        tvMemberPrice.setText("");
                    }
                    if (TextUtils.isEmpty(specGroupList.get(i).getLogo())) {
                        GlideUtils.loadImage(this, model.getGoods().getLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                            @Override
                            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                            }

                            @Override
                            public void onLoadingError(String source, Exception e) {

                            }
                        }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                    } else {
                        GlideUtils.loadImage(this, specGroupList.get(i).getLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                            @Override
                            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                            }

                            @Override
                            public void onLoadingError(String source, Exception e) {

                            }
                        }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                    }


                }


                tvPopChooseSpec.setText("已选择" + specSB);
            }


        } else {
            if (specGroupList.size() > 0)
                tvGoodsPrice.setText("￥" + specGroupList.get(0).getGoodsSpecValuesPrice());
            if (specGroupList.get(0).isHaveMemberPriceFlag()) {
                tvMemberPrice.setText("会员价: ¥" + String.format("%.2f", specGroupList.get(0).getStoreMemberPrice()));
            } else {
                tvMemberPrice.setText("");
            }
        }


    }

    private void updatePostageView(TextView tv) {
        selectPostageId = (int) tv.getTag();
        for (int i = 0; i < tvPostageTabs.size(); i++) {
            int tag = (int) tvPostageTabs.get(i).getTag();
            if (tag == selectPostageId) {
                tvPostageTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeColor));
                tvPostageTabs.get(i).setBackgroundResource(R.drawable.shape_app_theme_frame);
            } else {
                tvPostageTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeSubTitleColor));
                tvPostageTabs.get(i).setBackgroundResource(R.drawable.shape_gray_frame);
            }

        }
    }

    private void initPopTabs(TextView tvGoodsPrice, TextView tvPopChooseSpec, TextView tvGoodsCount, ImageView ivPopGoodsDetail, CartGoodsSpecModel model, TextView tvMemberPrice) {
        tvGoodsCount.setText(buyCount + "");
        for (int i = 0; i < tvTabs.size(); i++) {
            CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean tabsTag = (CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean) tvTabs.get(i).getTag();
            boolean selected = tabsTag.isSelected();
            if (selected) {
                selectSpecValueListBean.add(tabsTag);
                tvTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeColor));
                tvTabs.get(i).setBackgroundResource(R.drawable.shape_app_theme_frame);
            } else {
                tvTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeSubTitleColor));
                tvTabs.get(i).setBackgroundResource(R.drawable.shape_gray_frame);
            }

        }
        Collections.sort(selectSpecValueListBean, new SortBySpecId());
        StringBuilder keyGroup = new StringBuilder();
        StringBuilder specSB = new StringBuilder();
        for (int i = 0; i < selectSpecValueListBean.size(); i++) {
            if (i == selectSpecValueListBean.size() - 1) {
                keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId());
                specSB.append(selectSpecValueListBean.get(i).getSpecValue());
            } else {
                keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId() + ",");
                specSB.append(selectSpecValueListBean.get(i).getSpecValue() + ",");
            }
        }
        if (selectSpecValueListBean.size() > 0) {
            for (int i = 0; i < specGroupList.size(); i++) {
                if ((keyGroup + "").equals(specGroupList.get(i).getGoodsSpecId())) {
                    tvGoodsPrice.setText("￥" + specGroupList.get(i).getGoodsSpecValuesPrice());
                    tvGoodsPrice.setText("￥" + specGroupList.get(i).getGoodsSpecValuesPrice());
                    if (specGroupList.get(i).isHaveMemberPriceFlag()) {
                        tvMemberPrice.setText("会员价: ¥" + String.format("%.2f", specGroupList.get(i).getStoreMemberPrice()));
                    } else {
                        tvMemberPrice.setText("");
                    }
                    if (TextUtils.isEmpty(specGroupList.get(i).getLogo())) {
                        GlideUtils.loadImage(this, model.getGoods().getLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                            @Override
                            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                            }

                            @Override
                            public void onLoadingError(String source, Exception e) {

                            }
                        }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                    } else {
                        GlideUtils.loadImage(this, specGroupList.get(i).getLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                            @Override
                            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                            }

                            @Override
                            public void onLoadingError(String source, Exception e) {

                            }
                        }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                    }


                }
                tvPopChooseSpec.setText("已选择" + specSB);
            }
        } else {
            if (specGroupList.size() > 0) {
                tvGoodsPrice.setText("￥" + specGroupList.get(0).getGoodsSpecValuesPrice());
                if (specGroupList.get(0).isHaveMemberPriceFlag()) {
                    tvMemberPrice.setText("会员价: ¥" + String.format("%.2f", specGroupList.get(0).getStoreMemberPrice()));
                } else {
                    tvMemberPrice.setText("");
                }
                if (TextUtils.isEmpty(specGroupList.get(0).getLogo())) {
                    GlideUtils.loadImage(this, model.getGoods().getLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                        @Override
                        public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                        }

                        @Override
                        public void onLoadingError(String source, Exception e) {

                        }
                    }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                } else {
                    GlideUtils.loadImage(this, specGroupList.get(0).getLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                        @Override
                        public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                        }

                        @Override
                        public void onLoadingError(String source, Exception e) {

                        }
                    }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                }

            }

        }
        for (int i = 0; i < tvPostageTabs.size(); i++) {
            int tag = (int) tvPostageTabs.get(i).getTag();
            if (tag == selectPostageId) {
                tvPostageTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeColor));
                tvPostageTabs.get(i).setBackgroundResource(R.drawable.shape_app_theme_frame);
            } else {
                tvPostageTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeSubTitleColor));
                tvPostageTabs.get(i).setBackgroundResource(R.drawable.shape_gray_frame);
            }


        }


    }

    @Override
    public void goodsSelect(List<GoodsEntity> settlement, List<GoodsEntity> delete) {
        settlementData = settlement;
        deleteData = delete;
        MyLog.e("asus", settlementData.size() + "--->" + deleteData.size());
        double price = 0.0;
        if (isShown) {
            tvSettleDelete.setText("删除(" + deleteData.size() + ")");
            for (int i = 0; i < deleteData.size(); i++) {
                if (deleteData.get(i).isHaveMemberPriceFlag()) {
                    price += deleteData.get(i).getStoreMemberPrice() * deleteData.get(i).getCount();
                } else {
                    price += deleteData.get(i).getGoodsSpecValuesPrice() * deleteData.get(i).getCount();
                }


            }
            if (deleteData.size() > 0) {
                if (deleteData.containsAll(storeModels) && deleteData.containsAll(platformModels)) {
                    isDeleteCheckAll = true;
                    cbCheckAll.setChecked(true);
                } else {
                    isDeleteCheckAll = false;
                    cbCheckAll.setChecked(false);
                }
            }
        } else {
            tvSettleDelete.setText("去结算(" + settlementData.size() + ")");
            for (int i = 0; i < settlementData.size(); i++) {
                if (settlementData.get(i).isHaveMemberPriceFlag()) {
                    price += settlementData.get(i).getStoreMemberPrice() * settlementData.get(i).getCount();
                } else {
                    price += settlementData.get(i).getGoodsSpecValuesPrice() * settlementData.get(i).getCount();
                }


            }
            if (settlementData.size() > 0) {
                if (settlementData.containsAll(storeModels) && settlementData.containsAll(platformModels)) {
                    isSettleCheckAll = true;
                    cbCheckAll.setChecked(true);
                } else {
                    isSettleCheckAll = false;
                    cbCheckAll.setChecked(false);
                }
            }

        }
        tvGoodsPriceTotal.setText("￥" + String.format("%.2f", price));

    }

    @Override
    public void checkAllChange(boolean isPopShow) {
        if (isPopShow) {
            isDeleteCheckAll = false;
        } else {
            isSettleCheckAll = false;
        }
        cbCheckAll.setChecked(false);
    }

    class SortBySpecId implements Comparator<CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean> {
        @Override
        public int compare(CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean o1, CartGoodsSpecModel.SelectedSpecListBean.SpecValueListBean o2) {
            return o1.getSpecId() - o2.getSpecId();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case SELECT_STORE_TYPE:
                    isChooseServiceStore = true;
                    llNoBindStore.setVisibility(View.GONE);
                    llBindStore.setVisibility(View.VISIBLE);
                    ivSelectStore.setVisibility(View.VISIBLE);
                    SelectStoreModel selectStoreModel = (SelectStoreModel) data.getSerializableExtra(Constants.TRANSMISSION_CONTENT_KEY);
                    storeServicePrice = "参考安装费: " + "<font color='#09c0c7'>" + "￥" + selectStoreModel.getServicePrice() + "</font>";
                    storeName = "服务门店:" + selectStoreModel.getStoreName();
                    tvServiceStore.setText(storeName);
                    tvInstallMoney.setText(Html.fromHtml(storeServicePrice));
                    serviceInstallStoreId = selectStoreModel.getStoreId();
                    break;
                case SETTLE_TYPE:
                    //getCartList();
                    finish();
                    break;
            }
        }
    }

    private void AddToCartRequest(CartGoodsSpecModel model) {
        boolean isCanDissmiss = true;
        AddCartApi addCartApi = new AddCartApi(new HttpOnNextListener<String>() {
            @Override
            public void onNext(String o) {
                getCartList();
                tvSettleDelete.setText("删除(0)");
                PhoneUtils.ShowToastMessage(ShoppingCartActivity.this, "修改商品成功");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        }, this);


        if (platformModels != null && platformModels.size() > 0) {
            for (int i = 0; i < platformModels.size(); i++) {
                if (platformModels.get(i).getItemFromList() != null && platformModels.get(i).getItemFromList().size() > 0) {
                    addCartApi.setFromType(platformModels.get(i).getItemFromList().get(0).getFromType() + "");
                    addCartApi.setFromTypeId(platformModels.get(i).getItemFromList().get(0).getFromTypeId() + "");
                    MyLog.e("yang", "setFromType==" + platformModels.get(i).getItemFromList().get(0).getFromType());
                    MyLog.e("yang", "setFromTypeId==" + platformModels.get(i).getItemFromList().get(0).getFromTypeId());
                }

            }


        }


        addCartApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        addCartApi.setGoodsId(model.getGoods().getGoodsId() + "");
        addCartApi.setCount(buyCount + "");
        if (selectPostageId != 0) {
            addCartApi.setDeliveryWayId(selectPostageId + "");
        }
        //选择无需-1
        if (isInstallShow) {
            if (serviceInstallStoreId != 0 && needInstall) {
                addCartApi.setServiceInstallStoreId(serviceInstallStoreId + "");
                addCartApi.setNeedService("true");
            } else if (serviceInstallStoreId == 0 && needInstall) {
                isCanDissmiss = false;
                PhoneUtils.ShowToastMessage(ShoppingCartActivity.this, "请选择安装门店");
            } else {
                addCartApi.setNeedService("true");
            }


        } else {
            addCartApi.setNeedService("false");
        }
        addCartApi.setShoppingCartId(model.getGoods().getShoppingCartId() + "");

        if (specList.size() == 0) {
            addCartApi.setSpecGroupKey(model.getGoodsList().get(0).getSpecGroupKey() + "");
        } else {
            if (specList.size() == selectSpecValueListBean.size()) {
                StringBuilder keyGroup = new StringBuilder();
                for (int i = 0; i < selectSpecValueListBean.size(); i++) {
                    if (i == selectSpecValueListBean.size() - 1) {
                        keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId());
                    } else {
                        keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId() + ",");
                    }
                }
                for (int i = 0; i < specGroupList.size(); i++) {
                    if (specGroupList.get(i).getGoodsSpecId().equals((keyGroup + ""))) {
                        addCartApi.setSpecGroupKey(specGroupList.get(i).getSpecGroupKey() + "");
                    }
                }
            } else {
                isCanDissmiss = false;
                if (selectSpecValueListBean.size() == 0) {
                    PhoneUtils.ShowToastMessage(ShoppingCartActivity.this, "请选择商品" + specList.get(0).getSpecName());
                } else {
                    boolean specContainer = true;
                    for (int i = 0; i < specList.size() && specContainer; i++) {
                        int specId = specList.get(i).getSpecId();
                        for (int j = 0; j < selectSpecValueListBean.size(); j++) {
                            if (selectSpecValueListBean.get(j).getSpecId() == specId) {
                                break;
                            } else {
                                if (j == selectSpecValueListBean.size() - 1) {
                                    specContainer = false;
                                    PhoneUtils.ShowToastMessage(ShoppingCartActivity.this, "请选择商品" + specList.get(i).getSpecName());
                                    break;
                                }
                            }

                        }
                    }
                }
            }


        }
        if (isCanDissmiss) {
            popWnd.dismiss();
            HttpManager manager = HttpManager.getInstance();
            manager.doHttpDeal(addCartApi);
        }

    }

    public void getGoodsRemainCount() {
        GoodsRemainCountApi goodsRemainCountApi = new GoodsRemainCountApi(new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Intent intent = new Intent(ShoppingCartActivity.this, ConfirmOrderActivity.class);
                intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, (Serializable) settlementData);
                intent.putExtra(TRANSMISSION_GOODS_TYPE_KEY, CART);
                intent.putExtra("isShoppingCar", "1");
                startActivityForResult(intent, SETTLE_TYPE);
            }


            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
            }
        }, this);
        goodsRemainCountApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        StringBuilder goodsIds = new StringBuilder();
        StringBuilder specKeys = new StringBuilder();
        StringBuilder numbers = new StringBuilder();
        for (int i = 0; i < settlementData.size(); i++) {
            goodsIds.append((settlementData.size() == (i + 1)) ? settlementData.get(i).getGoodsId() + "" : settlementData.get(i).getGoodsId() + ",");
            specKeys.append((settlementData.size() == (i + 1)) ? settlementData.get(i).getSpecGroupKey() + "" : settlementData.get(i).getSpecGroupKey() + ",");
            numbers.append((settlementData.size() == (i + 1)) ? settlementData.get(i).getCount() + "" : settlementData.get(i).getCount() + ",");
        }

        goodsRemainCountApi.setGoodsIds(goodsIds + "");
        goodsRemainCountApi.setSpecGroupKeys(specKeys + "");
        goodsRemainCountApi.setNumbers(numbers + "");
        goodsRemainCountApi.setFormShoppingCar(true + "");
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(goodsRemainCountApi);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unRegister(this);
    }
}
